//
//  HSYPlayerBrightnessView.m
//  高仿暴走斗图
//
//  Created by key on 16/8/19.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "HSYBasePlayerIndicatorView.h"
#import "HSYBaseVideoPlayerView.h"
#import "NSObject+CreateControls.h"

static const CGFloat kMargin                                        = 1.0f;
static const CGFloat kBlockW                                        = 5.5f;
static const CGFloat kBlockH                                        = 2.75f;

#define BLOCKS_ARRAY_COUNT              16
#define HIDE_ANIMATION                  0.3f
#define BACK_GROUND_COLOR               [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]

@interface HSYBasePlayerIndicatorView ()

@property (nonatomic, strong) NSMutableArray *blocksArray;

@end

@implementation HSYBasePlayerIndicatorView

- (instancetype)initWithLogo:(UIImage *)logo highLogo:(UIImage *)highLogo indicatorType:(kHSYPlayerIndicatorProgessType)indicatorType
{
    self = [super initWithFrame:CGRectZero];
    if (self) {
        self.hidden = YES;
        self.layer.cornerRadius = kCornerRadius;
        self.layer.masksToBounds = YES;
        self.backgroundColor = BACK_GROUND_COLOR;
        
        _logo = logo;
        _indicatorType = indicatorType;
        _highLogo = highLogo;
        if (indicatorType == kHSYPlayerIndicatorProgessTypeBrightness || indicatorType == kHSYPlayerIndicatorProgessTypeVolume) {
            [self createIndicator];
        }
        [self configScreenObserver];
    }
    return self;
}

#pragma mark - Create UI

- (void)createIndicator
{
    [self brightnessLogo];
    [self brightnessProgess];
}

- (void)brightnessLogo
{
    // 亮度图标
    UIImageView *brightnessImageView = [NSObject createImgWithImage:self.logo
                                                             hImage:self.logo
                                                      clipsToBounds:YES
                                                            vcModel:UIViewContentModeScaleToFill];
    brightnessImageView.frame = CGRectMake((kVideoBrightnessIndicatorViewSide - 50) / 2, kViewSpacing, 50, 50);
    [self addSubview:brightnessImageView];
}

- (void)brightnessProgess
{
    // 亮度条
    self.blocksArray = [NSMutableArray arrayWithCapacity:BLOCKS_ARRAY_COUNT];
    
    UIView *blockBackgroundView = [NSObject createViewWithBgColor:[UIColor colorWithRed:0.25f green:0.22f blue:0.21f alpha:0.65]];
    blockBackgroundView.frame = CGRectMake((kVideoVolumeIndicatorViewSide - 105) / 2, 50 + kViewSpacing * 2, 105, 2.75 + 2);
    [self addSubview:blockBackgroundView];
    
    CGFloat margin = kMargin;
    CGFloat blockW = kBlockW;
    CGFloat blockH = kBlockH;
    
    for (int i = 0; i < BLOCKS_ARRAY_COUNT; i++) {
        CGFloat locX = i * (blockW + margin) + margin;
        UIImageView *blockView = [[UIImageView alloc] init];
        blockView.backgroundColor = [UIColor whiteColor];
        blockView.frame = CGRectMake(locX, margin, blockW, blockH);
        
        [blockBackgroundView addSubview:blockView];
        [self.blocksArray addObject:blockView];
    }
}

#pragma mark - Update Progess

- (void)updateIndicator:(CGFloat)value
{
    self.hidden = NO;
    // 防止重叠显示
    if (self.superview.accessibilityIdentifier) {
        [self toPreventOverlap];
    } else {
        self.superview.accessibilityIdentifier = @"";
    }
    
    CGFloat stage = kIndicatorAutoFadeOutTimeInterval / BLOCKS_ARRAY_COUNT;
    NSInteger level = value / stage;
    
    for (NSInteger i = 0; i < self.blocksArray.count; i++) {
        UIImageView *img = self.blocksArray[i];
        if (i <= level) {
            img.hidden = NO;
        } else {
            img.hidden = YES;
        }
    }
    [self updateUIConstraintByValue:value];
    [self cancelPreviousPerformSelector:@selector(animateHide)];
}

- (void)cancelPreviousPerformSelector:(SEL)selector
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:selector object:nil];
    [self performSelector:selector withObject:nil afterDelay:kIndicatorAutoFadeOutTimeInterval];
}

#pragma mark - Reset

- (void)toPreventOverlap
{
    return;
}

- (void)configScreenObserver
{
    return;
}

- (void)updateUIConstraintByValue:(CGFloat)value
{
    return;
}

#pragma mark - Animation

- (void)animateHide
{
    @weakify(self);
    [UIView animateWithDuration:[self duratioin] animations:^{
        @strongify(self);
        self.alpha = kMinValue;
    } completion:^(BOOL finished) {
        @strongify(self);
        self.hidden = YES;
        self.alpha = kMaxValue;
        self.superview.accessibilityIdentifier = nil;
    }];
}

- (NSTimeInterval)duratioin
{
    return HIDE_ANIMATION;
}

@end
